/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.ruralLibrary.api.dto.BookInfoEntityVo;
import com.pig4cloud.pigx.ruralLibrary.api.entity.*;
import com.pig4cloud.pigx.ruralLibrary.api.vo.RecommendManageVo;
import com.pig4cloud.pigx.ruralLibrary.mapper.RecommendManageMapper;
import com.pig4cloud.pigx.ruralLibrary.service.BookDataTypeService;
import com.pig4cloud.pigx.ruralLibrary.service.BookTypeService;
import com.pig4cloud.pigx.ruralLibrary.service.RecommendManageService;
import com.pig4cloud.pigx.ruralLibrary.service.ResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推荐位管理表
 *
 * @author pig
 * @date 2023-08-03 11:42:56
 */
@Service
public class RecommendManageServiceImpl extends ServiceImpl<RecommendManageMapper, RecommendManageEntity> implements RecommendManageService {

    @Autowired
    private ResourceService resourceService;

    @Override
    public R getByResources(String bookName) {
        List<RecommendManageVo> resources = new ArrayList<>();
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(ResourceEntity::getResourceName, bookName);
        List<ResourceEntity> list = resourceService.list(wrapper);
        if (list.size() == 0) {
            return R.failed("找不到此图书资源");
        }
        for (ResourceEntity resourceEntity : list) {
            RecommendManageVo recommendManageVo = new RecommendManageVo();
            recommendManageVo.setResourceId(resourceEntity.getResourceId());
            recommendManageVo.setResourceName(resourceEntity.getResourceName());
            recommendManageVo.setAuthor(resourceEntity.getAuthor());
            recommendManageVo.setCover(resourceEntity.getCover());
            recommendManageVo.setDataTypeId(resourceEntity.getDataTypeId());
            recommendManageVo.setBriefIntroduction(resourceEntity.getBriefIntroduction());
            recommendManageVo.setBookTypeId(resourceEntity.getBookTypeId());
            resources.add(recommendManageVo);
        }
        return R.ok(resources);
    }

    @Override
    public R saveEntity(RecommendManageEntity recommendManage) {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RecommendManageEntity::getResourceId, recommendManage.getResourceId());
        long count = this.count(wrapper);
        if (count != 0) {
            return R.failed("重复推荐资源");
        }
        return R.ok(this.save(recommendManage));
    }

    @Override
    public R getList() {
        LambdaQueryWrapper<RecommendManageEntity> wrapper = Wrappers.lambdaQuery();
        long totalCount = this.count(wrapper);

        int pageSize = 12;

        long totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        List<List<RecommendManageEntity>> lists = new ArrayList<>();
        for (int i = 1; i <= totalPages; i++) {
            // 分页查询
            List<RecommendManageEntity> pageList = this.page(new Page<>(i, pageSize), wrapper).getRecords();
            // 将当前页的数据放到子列表中
            List<List<RecommendManageEntity>> subLists = splitList(pageList, pageSize);
            // 将子列表加入到结果列表中
            lists.addAll(subLists);
        }
        return R.ok(lists);
    }
    private <T> List<List<T>> splitList(List<T> list, int pageSize) {
        List<List<T>> result = new ArrayList<>();
        int count = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
        for (int i = 0; i < count; i++) {
            int start = i * pageSize;
            int end = Math.min((i + 1) * pageSize, list.size());
            result.add(list.subList(start, end));
        }
        return result;
    }

    @Override
    public R timeSequence() {
        LambdaQueryWrapper<RecommendManageEntity> Wrapper = Wrappers.lambdaQuery();
        Wrapper.orderByAsc(RecommendManageEntity::getPosition);
        long totalCount = 24l;

        int pageSize = 12;

        long totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        List<List<RecommendManageEntity>> lists = new ArrayList<>();
        for (int i = 1; i <= totalPages; i++) {
            // 分页查询
            List<RecommendManageEntity> pageList = this.page(new Page<>(i, pageSize), Wrapper).getRecords();
            // 将当前页的数据放到子列表中
            List<List<RecommendManageEntity>> subLists = splitList(pageList, pageSize);
            // 将子列表加入到结果列表中
            lists.addAll(subLists);
        }
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(ResourceEntity::getCover, "http://61.163.243.7:9999/digitallibrary/file/lengleng/暂无封面.jpg");
        wrapper.not(r -> r.isNull(ResourceEntity::getBriefIntroduction));
        wrapper.orderByDesc(ResourceEntity::getCreateTime);
        wrapper.last("LIMIT 2");
        List<ResourceEntity> list = resourceService.list(wrapper);
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap();
        map.put("time", list);
        map.put("pageView", lists);
        mapList.add(map);
        return R.ok(mapList);
    }
}
